// component/lifeForm/lifeForm.js
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {

	},

	/**
	 * 组件的初始数据
	 */
	data: {
		scroll_height: 900,
		isubmit: true, // 还有学生没有发送 
		TabTitle: true,
		tempFilePaths: [],
		currentId: 0,
		studeentList: [{
				id: 0,
				name: '未发送'
			},
			{
				id: 1,
				name: '已发送'
			}
		],
		files: [],
		activeIndex: 0,
		checkboxItems: [{
			name: 'all',
			value: '全选',
			checked: 'true'
		}, ],
		array: ['未按时吃饭', '无特殊情况', '未认真吃饭', '未按时睡觉', '睡觉吵闹'],
		situationIndex: 1,
		indicatorDots: false,
		autoplay: false,
		interval: 2000,
		duration: 100,
		pics: [], //本地上传图片的数组
		urlPics: [], //转换为url的数组
		unsendStudentList: [], //一开始加载界面的学生的名单
		sendStudentList: [],
		studentId: '', //发送和未发送那里的学生ID
		SendchildrenId: [], //选择发送的学生ID
		inpText: '还有需要特别说明的吗？', //要什么要说的咩
		inpTextarea: '还有需要特别说明的吗？',
		defalut: '../../image/signIn/bg2.png',
		current: 0,
		ismodel: true, // 是否要输入框
		isscroll: true
	},

	/**
	 * 组件的方法列表
	 */
	methods: {
		onShow: function() {
			console.log("我出来了")
			let that = this
			let sellPhoneInfo = app.globalData.sellPhoneInfo
			console.log(sellPhoneInfo)
			that.setData({
				scroll_height: 650
			})
		},
		onLoad: function() {
			var that = this;
			var time = util.formatTime(new Date());
			let sendEdId = [];
			this.setData({
				time: time
			});
			//还没有发送的学生
			wx.getStorage({
				key: 'teacherInfo',
				success: function(res) {
					console.log(res)
					organizationId = res.data.organizationId
					staffId = res.data.staffId
					that.getNotStudent()
					that.getSendedStufent()
				},
			})
		},
		// 获取已经发送的学生
		getSendedStufent: function() {
			let that = this
			wx.request({
				url: getStudentEatListUrl,
				data: {
					"organizationId": organizationId,
					"sendMessageType": "SENT",
					"staffId": staffId
				},
				method: 'POST',
				dataType: 'json',
				responseType: 'text',
				success: function(res) {
					console.log(res.data.msg)
					console.log(res.data.data)
					sendStudentList = res.data.data

					that.setData({
						sendStudentList: sendStudentList,
					})
				}
			})
		},
		// 获取还没有发送的学生
		getNotStudent: function() {
			let that = this
			wx.request({
				url: getStudentEatListUrl,
				data: {
					"organizationId": organizationId,
					"sendMessageType": "UNSENT",
					"staffId": staffId
				},
				method: 'POST',
				dataType: 'json',
				responseType: 'text',
				success: function(res) {
					let studentList = res.data.data
					if (studentList.length == 0) {
						that.setData({
							isubmit: false
						})
					}
					console.log(studentList)
					for (let i = 0; i < studentList.length; i++) {
						studentList[i].isChange = true
					}
					for (let i = 0; i < res.data.data.length; i++) {
						SendchildrenId.push(res.data.data[i].id)
					}
					that.setData({
						unsendStudentList: studentList,
						SendchildrenId: SendchildrenId
					})
				}
			})
		},
		//上传图片
		chooseImage: function(e) {
			var that = this;
			var pics = this.data.pics
			wx.chooseImage({
				count: 3,
				sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
				success: function(res) {
					// 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
					console.log(res.tempFilePaths)
					tempFilePaths = res.tempFilePaths
					var imgsrc = res.tempFilePaths;
					pics = pics.concat(imgsrc);
					that.setData({
						files: that.data.files.concat(res.tempFilePaths),
						pics: pics
					});
				}
			})
		},
		//预览即将上传的东西
		previewImage: function(e) {
			wx.previewImage({
				current: e.currentTarget.id, // 当前显示图片的http链接
				urls: this.data.files // 需要预览的图片http链接列表
			})
		},
		// 选择情况
		bindPickerChange: function(e) {
			console.log('picker发送选择改变，携带值为', e.detail.value)
			this.setData({
				situationIndex: e.detail.value
			})
		},
		checkboxChange: function(e) {
			var checked = e.detail.value
			var changed = {}
			for (var i = 0; i < this.data.checkboxItems.length; i++) {
				if (checked.indexOf(this.data.checkboxItems[i].name) !== -1) {
					changed['checkboxItems[' + i + '].checked'] = true
				} else {
					changed['checkboxItems[' + i + '].checked'] = false
				}
			}
			this.setData(changed)
		},
		// 发送未发送
		sendChange: function(e) {
			this.setData({
				currentId: e.detail.current
			})
			console.log(e.detail.current)
		},
		// 点击孩子照片未发送
		changeUnsend: function(e) {
			let that = this
			let key = e.currentTarget.dataset.key
			let id = e.currentTarget.dataset.id
			let unsendStudentList = that.data.unsendStudentList
			let SendchildrenId = that.data.SendchildrenId
			console.log(e.currentTarget.dataset.ischange)
			unsendStudentList[key].isChange = !unsendStudentList[key].isChange // 选择不选择呢？
			console.log(key)
			console.log(id)
			//如果这个元素存在，就删除，如果不存在就添加
			let index = SendchildrenId.indexOf(id)
			switch (index) {
				case -1:
					SendchildrenId.push(id)
					break;
				default:
					SendchildrenId.splice(index, 1)
			}
			this.setData({
				studentId: e.currentTarget.dataset.id,
				unsendStudentList: unsendStudentList,
				SendchildrenId: SendchildrenId
			})
		},
		//有什么要说的咩
		inpText: function(e) {
			console.log(e.detail.value)
			this.setData({
				inpText: e.detail.value
			})
		},
		submit: function() {
			wx.showLoading({
				title: '加载中',
			})
			let that = this
			let organizationId, staffId;
			let inpText = that.data.inpText;
			let SendchildrenId = that.data.SendchildrenId;
			let degree = 0;
			// 获取id
			for (var i = 0; i < that.data.pics.length; i++) {
				wx.uploadFile({
					url: getImgUrl,
					filePath: that.data.pics[i],
					name: 'file',
					formData: null,
					success: (resp) => {
						console.log(JSON.parse(resp.data).data[0])
						that.data.urlPics.push(JSON.parse(resp.data).data[0])
						degree++;
						if (that.data.pics.length == degree) {
							that.uploadInfo(inpText, organizationId, staffId, SendchildrenId, that.data.urlPics)
						}

					},
					fail: (res) => {
						console.log(JSON.parse(res.data.msg))
						if (msg == "失败") {
							return false
						}
					}
				});
			}
		},
		// 上传照片获取
		// 上传员工信息
		uploadInfo: function(inpText, organizationId, staffId, SendchildrenId, urlPics) {
			wx.request({
				url: addStudentEatRecordUrl,
				method: 'POST',
				data: {
					"desc": inpText,
					"organizationId": organizationId,
					"staffId": staffId,
					"studentIds": SendchildrenId,
					"urls": urlPics
				},
				success: function(res) {
					wx.hideLoading()
					console.log(res)
					if (res.data.msg == "成功") {
						wx.hideLoading()
						wx.showToast({
							title: '发送成功',
							icon: 'success',
							duration: 2000
						})
					}
				},
				fail: function(res) {
					console.log(res)
				},
				complete: function() {
					console.log("我出现过")
				}
			})
		},
		//没有可以发送的学生的时候弹出
		nosubmit: function() {
			wx.showModal({
				title: '午餐情况已经发送完了',
				content: '你还想干啥子嘛！！！！',
				success(res) {
					if (res.confirm) {
						wx.switchTab({
							url: '../../../../tabBar/mainFunction/mainFunction'
						})
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				}
			})
		},
		TabNoonSleep: function() {
			let that = this;
			that.setData({
				current: 1
			})
			wx.setNavigationBarTitle({
				title: '午睡情况填写'
			})
		},
		TabNoonEat: function() {
			let that = this
			that.setData({
				current: 0
			})
			wx.setNavigationBarTitle({
				title: '拍摄午餐情况'
			})

		},

	}
})
